<?php

namespace app\common\controller;

use app\common\cache\SubQueue;
use app\common\traits\ControllerTrait;
use app\common\event\UserLog;
use Helper\ResponseHelper;
use think\App;
use think\C;
use think\facade\Request;

/**
 * 基础控制器
 * Class BaseController
 * @package common\controller
 */
class BaseController extends \app\BaseController
{
    use ControllerTrait;

    /**
     * @var array|string
     */
    public $param;

    /**
     * BaseController constructor.
     * @param App|null $app
     */
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $param = Request::param();
        $this->param = $this->formatParam($param);
        $this->app = $app;
    }

    /**
     * 格式化参数
     * @param $param
     * @return array|string
     */
    public function formatParam($param)
    {
        $except_arr = ['create_time', 'update_time', 'creater_id', 'updater_id'];
        if (is_array($param)) {
            foreach ($param as $key => $value) {
                //过滤空字符串
                $param[$key] = $this->formatParam($value);
                //去除自动完成的字符串,这里注意数组的零键和数组相等
                if (in_array($key, $except_arr, true)) {
                    unset($param[$key]);
                }
            }
        } else {
            $param = trim($param);
        }
        return $param;
    }

    /**
     * 返回一个代表成功的json数据 <br>['code' => 0, 'msg' => 'success', 'data' => $data]
     * @param array $data
     * @return \think\response\Json
     */
    public function json_responce($data = [])
    {
        $ret = ResponseHelper::getResponseExample($data);
        event(UserLog::class, $ret);
        return json($ret);
    }

}
